import "../style/wj.css";
import { getRequestParameter } from "./request";
import { EUV } from "@hongweifei/euv";
import { StatusCode } from "../api/web-result";
import { getUserInfo } from "../api/user";
import { getWJByUsernameAndTitle } from "../api/wj";

const parameters = getRequestParameter();
console.log(parameters);
if (!parameters["creator"] || !parameters["title"]) {
    alert("参数错误");
    window.history.back();
    window.location.href = "../index.html";
}


new EUV({
    elementSelector: "#app",
    async mounted() {
        const userInfoResult = await getUserInfo();
        if (userInfoResult.code !== StatusCode.SUCCESS) {
            alert(userInfoResult.message);
            window.location.href = "../index.html";
            return;
        }
        const user = userInfoResult.data;
        if (!user) {
            alert("错误");
            window.location.href = "../index.html";
            return;
        }
        this.data.user = user;
        this.data.login = true;

        const wjResult = await getWJByUsernameAndTitle(parameters["creator"], parameters["title"]);
        if (wjResult.code === StatusCode.FAILED) {
            alert("问卷不存在");
            window.history.back();
            window.location.href = "../index.html";
            return;
        }
        this.data.wj = wjResult.data;
    },

    data: {
        user: {},
        login: false,

        wj: {},
        formData: {
            name: "",
            gender: "",
            age: "",
            phone: "",
        },

        历史提交: []
    },

    methods: {
        更新历史提交() {
            let info = [];
            const infoString = localStorage.getItem("信息");
            if (infoString) {
                try {
                    info = JSON.parse(infoString);
                } catch (e) {
                    console.error(e);
                }
            }
            if (info) {
                this.data.历史提交 = info;
            }
        },

        async submit问卷(e) {
            e.preventDefault();
            console.log(this.data.formData);
            if (!this.data.formData) {
                alert("请填写完整");
                return;
            }

            let info = [];
            const infoString = localStorage.getItem("信息");
            if (infoString) {
                try {
                    info = JSON.parse(infoString);
                } catch (e) {
                    console.error(e);
                }
            }
            info.push(this.data.formData);
            localStorage.setItem("信息", JSON.stringify(info));
            alert("提交成功");
            for (const key of Object.keys(this.data.formData)) {
                this.data.formData[key] = "";
            }
        },


        dialogOpen(open) {
            const dialog = this.$refs["dialog"];
            if (dialog) {
                if (!open) {
                    dialog.open = false;
                    return;
                }

                if (typeof dialog.showModel === "function") {
                    dialog.showModel();
                } else {
                    this.methods.更新历史提交.bind(this)();
                    dialog.open = true;
                }
            }
        },

        关闭历史提交() {
            this.methods.dialogOpen.bind(this)(false);
        },

        查看历史提交() {
            this.methods.dialogOpen.bind(this)(true);
        },

        清空历史() {
            localStorage.removeItem("信息");
        }
    }
});


